//#include "PLT_HEADS.h"
#include "TK_TdsChange.h"
/*---------------------------------------------------------------------------------------------------------*/
#define TEMP_INTERVAL   5    //基准曲线温度间隔
#define TDS_MAX         999  //TDS最大数值
#define TDS_MIN         1    //TDS最小数值
#define SCALE_A         1000 //2次项系数放大倍数
#define SCALE_B         10   //1次项系数放大倍数
/*---------------------------------------------------------------------------------------------------------*/
typedef struct 
{
    signed int a;      // 二次项系数
    signed int b;      // 一次项系数
    signed int c;      // 常数项
    signed int temp;   // 温度值
} TdsCoefficient;
static const TdsCoefficient Tds_Coefficient[] = {
    {1053, 1748,  414,  5},
    { 637, 1657, -255, 10},
    { 756, 1371, -409, 15},
    { 601, 1271,  556, 20},
    { 649,  915,  999, 25},
    { 575,   84, 1201, 30},
    { 568,  763, 2072, 35},
    { 532,  747, 1348, 40}
};
/*---------------------------------------------------------------------------------------------------------*/
/**
 * @brief 根据当前温度和AD值计算TDS ppm值
 * @param ad_value AD采样值（电压或频率）最大范围 0-1023 需采用10位的AD采样
 * @param coeff    TDS温度对照表 
 * @return TDS ppm值
 */
/*---------------------------------------------------------------------------------------------------------*/
static unsigned int compute_tds_value(unsigned int ad_value, const TdsCoefficient *coeff)
{
    signed long s32_data[3];

    s32_data[0] = (signed long)ad_value * ad_value * coeff->a / SCALE_A;
    s32_data[1] = (signed long)ad_value * coeff->b / SCALE_B;
    s32_data[2] = coeff->c + s32_data[0] + s32_data[1];

    return (unsigned int)s32_data[2];
}
/*---------------------------------------------------------------------------------------------------------*/
/**
 * @brief 根据当前温度和AD值计算TDS ppm值
 * @param u16_PresentTemp 当前温度（单位：℃）
 * @param u16_AdValue AD采样值（电压或频率）最大范围 0-1023 需采用10位的AD采样
 * @return TDS ppm值，范围 [1, 999]
 */
/*---------------------------------------------------------------------------------------------------------*/
unsigned int calculate_tds_ppm(unsigned int u16_PresentTemp, unsigned int u16_AdValue)
{
    unsigned char i = 0, j = 0, k = 0;
    unsigned int u16_Tds1 = 0, u16_Tds2 = 0, u16_Tdsout = 0;
    // 查找匹配的基准温度索引
    for(i = 0; i < 8; i++)
    {
        if(u16_PresentTemp == Tds_Coefficient[i].temp)
        {
            j = i;
            k = 1;
            break;
        }
    }

    if(u16_PresentTemp > Tds_Coefficient[7].temp) {
        j = 7;
        k = 1;
    }

    if(k == 0)
    {
        for(i = 0; i < 8; i++)
        {
            if(u16_PresentTemp < Tds_Coefficient[i].temp) {
                j = i;
                break;
            }
        }
    }
    //高温度的TDS值
    u16_Tds1 = compute_tds_value(u16_AdValue,&Tds_Coefficient[j]);   

    // 温度补偿 
    if((j > 0) && (k == 0))
    {
        int temp_diff = 0;
        int tds_diff = 0;
        //低温度的TDS值
        u16_Tds2 = compute_tds_value(u16_AdValue,&Tds_Coefficient[j - 1]);  
        //计算当前温度与基准温度的温差
        temp_diff = Tds_Coefficient[j].temp - u16_PresentTemp;
        //TDS温度补偿后输出
        if(u16_Tds2 > u16_Tds1)
        {
            tds_diff = u16_Tds2 - u16_Tds1;
            
            u16_Tds2 = tds_diff * temp_diff / TEMP_INTERVAL;
            
            u16_Tds1 += u16_Tds2;
        }
        else
        {
            tds_diff = 0;
        }
    }

    // 输出限制
    if(u16_Tds1 > TDS_MAX) {
        u16_Tds1 = TDS_MAX;
    } else if(u16_Tds1 < TDS_MIN) {
        u16_Tds1 = TDS_MIN;
    }

    u16_Tdsout = u16_Tds1;
    return u16_Tdsout;
}